package evolution;

import ca.swank.hardquor.*;

/**
 * @author Alon Segal
 */
public class fitgenAIUser implements hardquorUser {
	public static final int AI_TYPE_LOOKAHEAD = 4;
	public static final int AI_TYPE_TREE = 5;
	private String userName;
	private hardquorClient client;
	private int selection;
	private double pMut;

	/**
	 * constructs a new aiUser which attempts to connect to the server as
	 * username
	 */
	public fitgenAIUser(String serverIp, String userName, int select, double pMut) {
		this.userName = userName;
		this.selection = select;
		this.pMut = pMut;
		client = new hardquorClient(this);
		client.connect(serverIp);
	}

	/**
	 * called upon connection to a hardquor game server. Upon having this method
	 */
	public void notifyRequestUserName(String reason) {
		System.out.println("username requested for reason: " + reason);
		client.tryUsername(userName);
	}

	/**
	 * provides a list of online users upon connection to a hardquor game.
	 */
	public void notifyUserList(String[] users) {
		System.out.println("received userlist: " + users);
	}

	/**
	 * notification that another user has disconnected from the hardquor game
	 */
	public void notifyUserGone(String username) {
		System.out.println("user gone: " + username);
	}

	/**
	 * notification that another user has connected to the current hardquor game
	 */
	public void notifyUserNew(String username) {
		System.out.println("user new: " + username);
	}

	/**
	 * a chat message received from the specified user. Chat messages should
	 */
	public void notifySpeak(String user, String message) {
		System.out.println(user + ": " + message);
	}

	/**
	 * an incoming game request from another user on the current hardquor
	 * server.
	 */
	public void notifyGameRequest(hardquorGameRequest request) {
		System.out.println("game requested");
		client.acceptGameRequest(request);
	}

	/**
	 * requests the user create an instance of an implementation of the
	 */
	public hardquorUserGame startGame(hardquorGameClient gameClient,
			int playerNumber) {
		System.out.println("game start");
		hardquorUserGame userGame = null;
		userGame = new GenAIGame(gameClient, playerNumber, userName, selection,
				pMut);
		return userGame;
	}

	public void startAIvsAI() {
		hardquorGameRequest gameRequest = new hardquorGameRequest(0, "GEN-FIT",
				33);
		client.makeGameRequest(gameRequest);
	}

	/**
	 * starts a new WalkerUser which accepts all requests on specified server
	 * and name
	 */
	public static void main(String[] args) {
		if (args.length != 4) {
			System.out
					.println("usage: aiUser [server_ip] [aiName] [selection] [maxmin]");
		} else {
			int value = 0;
			int maxmin = 0;
			char[] test = args[2].toCharArray();
			value = (int) test[0];
			if (value == 49) {
				value = 1;
			} else if (value == 50) {
				value = 2;
			} else {
				value = 3;
			}
			test = args[3].toCharArray();
			maxmin = (int) test[0];
			if (maxmin == 49) {
				maxmin = 1;
			} else if (value == 50) {
				maxmin = -1;
			}
			System.out.print("FITNESS*******");
			fitgenAIUser user = new fitgenAIUser(args[0], args[1], 1, 0.2);
		}
	}
}